//返回值为需不需要关闭窗口
function yesOperator(){
  var retValue=addData();
  return retValue;
}
function updateLoad(){
    var params = $$.getQueryString();

    $$.ajax({
        url:"/rest/IotDeviceBusiness/query",
        data : {
            id : params.rowId
        },
        success : function(data){
            $("#b_id").val(data.id);
            $("#b_deviceCode").val(data.deviceCode);
            $("#b_deviceName").val(data.deviceName);
            $("#b_devicePwd").val(data.devicePwd);
            $("#b_protocol").val(data.protocol);
            $("#b_protocolService").val(data.protocolService);
            $("#b_address").val(data.address);
            $("#b_rowSort").val(data.rowSort);
            $("#b_remarks_1").val(data.remarks);
            $("#b_status").val(data.status);
            $("#b_remarks").val(data.remarks);


            $("#b_deviceCodeOld").val(data.deviceCode);
            $("#b_statusOld").val(data.status);
            $("#b_devicePwdOld").val(data.devicePwd);


            var status=data.status;
            $("input[name^='b_status_']").each(function(){
                if($(this).val()==status){
                    this.checked='checked';
                }

            });


        }
    });



}
function validForm(){
    var ret= $("#theform").valid();
    return ret;
}
function addData(){
    var param = {};
    var validFormRet;
    $$.form({
        async:false,
        able:function(){
            validFormRet=false;
        },
        checkForm: validForm,
        success: function () {
            validFormRet= true;
        }
    });
    return validFormRet;
}

function deviceFormValid() {
    $("#theform").validate({
        rules: {
            b_deviceCode: {
                required: true,
                minlength: 3,
                isCode:true
            },
            b_deviceName: {
                required: true,
                minlength: 3,
            },
            b_devicePwd:{
                required: true,
                minlength: 2,
                maxlength: 15,
            },
            b_rowSort: {
                required: true,
                minlength: 1,
                isPositiveInteger: true
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips errortips-w200",
        success: 'valid',
        messages: {
            b_deviceCode: {
                required: " (必填)",
                minlength: " (不能少于 3 个字母)",
            },
            b_deviceName: {
                required: " (必填)",
                minlength: " (不能少于 3 个字母)",
            },
            b_devicePwd:{
                required: " (必填)",
                minlength: " (不能少于6位密码)"
            },
            b_rowSort:{
                isDigits: "输入正整数"
            }
        },
    });
}


$(function () {
    deviceFormValid();
    $("input[name^='b_status']").click(function(){
        $("#b_status").val($(this).val());
    });
});

jQuery.validator.addMethod("isCode", function(value, element) {
    if(!/^[A-Z]{3}[0-9]{4}$/.test($("#b_deviceCode").val())){
        $(element).data('error-msg','请输入3位大写字母＋4位数字');
        return false;
    }
    //校验编码是否存在
    var fieldName="deviceCode";
    var fieldNameCn="设备编码";
    var serviceName="IotDeviceBusiness";
    var Code=$("#b_"+fieldName).val();
    var CodeOld=$("#b_"+fieldName+"Old").val();
    if(Code==CodeOld){
      return true;
    }else{

        var args={};
        args.c_deviceCode_1=$("#b_"+fieldName+"").val();
        args.onlyQuery="1";
        args.page=0;
        args.pageSize=5;
        args.isPage=1;
        var exists;
        $$.ajax({
            url: "/bootstrap/basegrid/"+serviceName+"/list",
            async:false,
            data: args,
            sflag:false,
            success: function (data) {
                if(data.length && data.length>0){
                    exists=true;
                }else{
                    exists=false;
                }
            }
        });
        if(exists){
            $(element).data('error-msg','编码不能重复！');
            return false;
        }
        return true;
    }

}, function(params, element) {
    return $(element).data('error-msg');
});
